requirejs(['serializeObject', 'art-template', 'moment'], function (_, template, moment) {
    $(document).ready(function () {
    });

    $('#dg').datagrid({
        method: 'post',
        striped: true,
        singleSelect: true,
        pageSize: 20,
        border: false,
        toolbar: '#tb',
        fit: true,
        rownumbers: true,
        pagination: true,
        fitColumns: true,
        queryParams: $("#searchForm").serializeObject(),
        onDblClickRow: function (rowIndex, row) {
        },
        onLoadSuccess: function () {
        },
        url: ctx + "/study/homeworkDetail/listCommittedPage",
        columns: [[
            {field: 'studentName', title: '学生姓名', width: 150},
            {field: 'title', title: '标题', width: 300},
            {
                field: 'updateDate', title: '提交时间',
                formatter: function (val, row, index) {
                    if(val){
                        return moment(val).format(DATE_FORMAT.DATE_FORMAT_YMD);
                    }
                }
            },
            {field: 'remark', title: '备注', width: 150},
            {
                field: 'operate', title: '操作',
                formatter: function (val, row, index) {
                    var html = template("operateTemplate", row);
                    return html;
                }
            }
        ]]
    });

    /**
     * 查询
     */
    $("#searchButton").click(function () {
        var data = $("#searchForm").serializeObject();
        $('#dg').datagrid('load', data);
    });

    /**
     * 监听回车事件,触发查询
     */
    $("#searchForm,#searchForm input").bind("keydown", function (e) {
        // 兼容FF和IE和Opera
        var theEvent = e || window.event;
        var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
        if (code == 13) {
            $("#searchButton").click();
        }
    });

    /**
     *  清空查询条件
     */
    $("#clearButton").click(function () {
        $('#searchForm').form('reset');
        var data = $("#searchForm").serializeObject();
        $('#dg').datagrid('load', data);
    });

    /**
     * 查看作业
     */
    $(document).on('click', ".js-viewButton", function () {
        var homeworkDetailId = $(this).data('id');
        var url = ctx + "/study/homeworkDetail/page/view/teacher?id=" + homeworkDetailId;
        window.parent.addModel("查看作业详情", url);
    });
});

